/**
 * Created with JetBrains PhpStorm.
 * User: kk
 * Date: 13-8-28
 * Time: 下午4:44
 */ 
$(function () {
	//屏蔽热键 F5
	$(window).keydown(function(e){
		if(e.keyCode==116){
			document.location.reload();
			return false;
		}
	});
	//兼容ie
	document.onkeydown=function(){
		if(window.event){
			if(event.keyCode==116){
				window.event.keyCode=0;  
				window.event.returnValue=false;
				document.location.reload();
			}
		}		
	}
    //操作确认
    $('.jsconfirm').click(function(){
        var self = $(this) ,sText = self.attr('title');
		parent.jsconfirm(sText+"？" ,
			function(){
				$.getJSON(self.attr('url'),function(data){
					if(data.err==0){
						SendWebMsg('success', data.msg);
                        window.location.reload();
						//ts('删除成功');
					}else{
					    SendWebMsg('error', data.msg);   
						//parent.jsshow('删除失败','','error');
					}
				})
			} ,'info');
		return false;
    });
	//删除单条数据
	$('.delete').click(function(){
		var self = $(this);
		parent.jsconfirm("确定要删除吗？" ,
			function(){
				$.getJSON(self.attr('url'),function(data){
					if(data.err==0){
					    SendWebMsg('success', data.msg);
						data.url ? window.location.href=data.url : $(self).parent().parent().remove();                        
						//ts('删除成功');
					}else{
						parent.jsshow(data.msg,'','error');
					}
				})
			} ,'info');
		return false;
	 });
	 //批量删除
	$('.deleteall').click(function(){
		var checked=$('.ids:checked') ,self = $(this);
		
		if(checked.length>0){
			parent.jsconfirm('确定要批量删除数据吗？' ,
				function(){
					var arr=new Array();
					$(".ids:checked").each(function(){
						arr.push($(this).val());
					});
					value_str=arr.join('|');
					$.post(self.attr('url'),{ids:value_str},function(data){
						var myObject = eval('(' + data + ')');
						if(myObject.err==0){
						    SendWebMsg('success', data.msg);  
							checked.parent().parent().remove();
							//ts('删除成功');
						}else{
							parent.jsshow(data.msg,'','error');
						}
					})
				} ,'info');
				
		}else{
			parent.jsshow('请选择要操作的选项','','error');
		}
		return false;
	 });
     //状态替换
     $('.ajaxOperateReplace').live('click' ,function(){
        var jqThis = $(this) ,url = jqThis.attr('url');
        $.getJSON(url ,function(jData){
            if( jData.err == 0 ){
                jqThis.replaceWith(jData.data.html);
                SendWebMsg('success' ,jData.msg);
            } else {
                SendWebMsg('error' ,jData.msg);
            }
            return false;
        });
     });
     //ajax排序
     $('.ajaxOperateSort').bind('change' ,function(){
        var jqThis = $(this) ,val = jqThis.val(), url = jqThis.attr('url')+'/sort/'+val,reg = /^[0-9]+$/;
        //验证VAL 是不是数字
        if( ! reg.test(val) ){
            SendWebMsg('error' ,'请输入正确的数字');
            jqThis.val('');
            jqThis.focus();
        }
        $.getJSON(url ,function(jData){
            if( jData.err == 0 ){
                SendWebMsg('success' ,jData.msg);
                window.location.reload();
            } else {
                SendWebMsg('error' ,jData.msg);
            }
            return false;
        });
     });
	//表格全选
	$(".btn_all").click(function(){
		if($(this).attr('checked')=='checked'){
			$("tbody input[type='checkbox']:enabled").attr("checked",true);
		}else{
			$("tbody input[type='checkbox']:enabled").attr("checked",false);
		}
	});
	$('.ids').click(function(){
		if($('.ids:checked').size()>0){
			$('.btn_all').attr('checked',true);
		}else{
			$('.btn_all').attr('checked',false);
		}
	});
	//
});

/*页面提示 扩展函数*/
function SendWebMsg(check,msg,url,reloadifame){
	var parents=$(window.parent.document).find('#iframe_load');
    if(!arguments[4]) reloadifame = "other";
	parents.hide();
	parents.find('span').html('请等待，加载中…');
	parent.addInfo(check,msg);
	if(url && reloadifame=='other' ){
		window.location.href=url;
	} else if ( url && reloadifame=='editpersonpass' ) {
	   window.parent.location.href = url;  
	}
}